package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.base.BaseWebActivity.LoadType;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnDismissListener;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnItemClickListener;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.library.widget.HSSipEditText;
import com.hundsun.yr.universal.p2p.bean.enums.OssidEnum;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.datahandler.RegisterValidateHandler;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import java.util.HashMap;

/**
 * @version V1.0
 * @Title: RegiesterActivity.java
 * @Description: 注册，已经获取验证码
 * @date 2015-12-3 上午9:57:20
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class RegisterValidateActivity extends BaseActivity implements OnClickListener, HSDialogOnDismissListener, HSDialogOnItemClickListener {
    private final static String TAG = "RegisterValidateActivity";
    public static final String REGISTER_PHONE = "REGISTERPHONE";
    private ValidateButton btn_getvalidate;
    private HSSharedPreferences shared;

    /**
     * 提交按钮
     */
    private HSButton btn_submit;
    /**
     * 注册验证码输入框
     */
    private HSEditText et_validate;
    /**
     * 注册密码输入框
     */
    private HSSipEditText et_password;
    /**
     * 推荐人手机号
     */
    private HSEditText et_referee;
    /**
     * 验证码
     */
    private String validateNum;
    /**
     * 手机号码
     */
    private String phoneNum;

    /**
     * 密码
     */
    private String passStr;
    /**
     * 推荐人手机
     */
    private String refereePhone;
    /**
     * 协议
     **/
    private TextView tv_protocol = null;
    /**
     * 协议选择
     **/
    private CheckBox ck_agree = null;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        phoneNum = extras.getString(Extras.PHONE);
//        validateNum = extras.getString(Extras.REGIEST_SMS);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_register_step2_validate;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        Logs.e("aa");
        findView();
        ck_agree.setChecked(false);
        shared = new HSSharedPreferences(this, SPPath.file_configInfo);
        setToolbarTitle(R.string.regiester_space);
        // 初始化获取验证码按钮
        btn_getvalidate.init(TAG);
        // 获取保存在后台的手机号
        String savedphone = shared.getShareProf(REGISTER_PHONE);

        if (!TextUtils.isEmpty(savedphone) && !TextUtils.equals(savedphone, phoneNum)) {
            btn_getvalidate.reset();
            shared.putShareProf(REGISTER_PHONE, "");
        }

        btn_getvalidate.onClick(btn_getvalidate);
        btn_getvalidate.setOnClickListener(this);

        btn_submit.setOnClickListener(this);
        mDataHandler = new RegisterValidateHandler(mContext);
    }

    private void findView() {
        btn_getvalidate = findView(R.id.register_btn_validate);
        btn_submit = findView(R.id.btn_register_submit);
        et_validate = findView(R.id.register_et_validate);
        et_password = findView(R.id.et_register_pass);
        et_referee = findView(R.id.et_register_referee);

        ck_agree = findView(R.id.ck_agree);
        tv_protocol = findView(R.id.tv_protocol);
        tv_protocol.setText(getString(R.string._protocol, OssidEnum.REGIST_PROTOCOL.getOssLabel()));
        tv_protocol.setOnClickListener(this);

    }

    @Override
    protected void onNetworkConnected(NetType type) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onNetworkDisConnected() {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onDestroy() {
        btn_getvalidate.onDestroy();
        shared.putShareProf(REGISTER_PHONE, phoneNum);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_btn_validate://发送验证码，多携带一个参数
                mDataHandler.interactiveSilent(mDataHandler, R.string.method_sendSms, phoneNum, " ");
                break;
            case R.id.btn_register_submit://发送注册请求
                if (checkPass()) {
                    Logs.e("passStr: " + passStr);
                    mDataHandler.interactive(mDataHandler, R.string.method_doRegister, phoneNum, passStr, validateNum, refereePhone);
                }
                break;

            case R.id.tv_protocol://注册协议
                String zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap mHeader = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET,getString(R.string.h5_protocol)+ TaskConstants.TASK_REGISTPROT,
                        "",mConfigSP.getShareProf(TaskConstants.TASK_CONFIG),zwgt_data);
                HeaderMap headerMap = new HeaderMap();
                headerMap.setMap(mHeader);

                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, OssidEnum.REGIST_PROTOCOL.getOssLabel());
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, mContext.getString(R.string.root_h5_url_test) + getString(R.string.h5_protocol)+ TaskConstants.TASK_REGISTPROT);
                bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER,headerMap);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, LoadType.HTTP.getIntValue());
                bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                readyGo(WebProtocolActivity.class, bundle);

                break;
            default:
                break;
        }

    }

    /**
     * 输入框校验
     *
     * @return
     */
    private boolean checkPass() {
        validateNum = CommTool.getValue(et_validate);
        passStr = CommTool.getValue(et_password);
        refereePhone = et_referee.getPhone();
        if (TextUtils.isEmpty(validateNum)) {
            showToast(R.string.validate_emptycheck);
        } else if (TextUtils.isEmpty(passStr)) {
            showToast(R.string.register_pass_emptycheck);
        } else if (validateNum.length() < 6) {
            showToast(R.string.validate_rightcheck);
        } else if (!CommTool.checkPwd(passStr)) {
            showToast(R.string.register_pass_rightcheck);
        } else if (!ck_agree.isChecked()) {
            showToast(getString(R.string.tips_agree_protocol, OssidEnum.REGIST_PROTOCOL.getOssLabel()));
        } else if (refereePhone.length() == 0 || CommTool.checkMobile(refereePhone)) {
            return true;
        } else {
            showToast(R.string.refereePhone_rightcheck);
        }

        return false;
    }


    @Override
    public void onDismiss(Object o) {

    }


    @Override
    public void onItemClick(Object o, int position) {
        if (position == 0) {
//            readyGo(BindNameActivity.class);
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        btn_getvalidate.reset();
        btn_getvalidate.onDestroy();
    }
}
